﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Xml;
using shop.cart;
using shop;

namespace comix
{
    public partial class main : System.Web.UI.MasterPage
    {

        Cart cart;
        CartDataObject cartObject;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            cart = (Cart)Session["cart"];

            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            XmlWriter xw = XmlWriter.Create(sb1);
            xw.WriteStartElement("cart");
            foreach (CartItem ci in cart.Items)
            {
                xw.WriteStartElement("cartItem");
                xw.WriteAttributeString("id", ci.Item.ItemId().ToString());
                xw.WriteAttributeString("price", ci.FinalPrice.ToString());
                xw.WriteAttributeString("desc", ci.Item.CartName());
                xw.WriteEndElement();
                sb.Append(ci.Item.ItemId());
                sb.Append(":");
                sb.Append(ci.FinalPrice);
                sb.Append("!");
            }
            xw.WriteEndElement();
            xw.Close();
            xmlcartdata.Value = sb1.ToString();
            
            if (sb.Length > 0) 
                sb.Remove(sb.Length - 1, 1);
            cartdata.Value = sb.ToString();
            cartsum.Value = cart.TotalPrice().ToString();
            cartObject = new CartDataObject(cart);
            SetImage();
        }

        protected void sortingmap_Click(object sender, ImageMapEventArgs e)
        {
            Session["sortingType"] = Convert.ToInt32(e.PostBackValue);
            SetImage();
        }

        private void SetImage()
        {
            alphabut.ImageUrl = "~/graphics/alphaoff.png";
            hmbut.ImageUrl = "~/graphics/hmoff.png";
            ratebut.ImageUrl = "~/graphics/rateoff.png";
            switch ((int)Session["sortingType"])
            {
                case 1:
                    alphabut.ImageUrl = "~/graphics/alphaon.png";
                    break;
                case 2:
                    hmbut.ImageUrl = "~/graphics/hmon.png";
                    break;
                case 3:
                    ratebut.ImageUrl = "~/graphics/rateon.png";
                    break;
            }
        }

        protected void alphabut_Command(object sender, CommandEventArgs e)
        {
            Session["sortingType"] = 1;
            SetImage();
        }

        protected void hmbut_Command(object sender, CommandEventArgs e)
        {
            Session["sortingType"] = 2;
            SetImage();
        }

        protected void ratebut_Command(object sender, CommandEventArgs e)
        {
            Session["sortingType"] = 3;
            SetImage();
        }

        protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = cartObject;
        }

        protected void LinkButton1_Command(object sender, CommandEventArgs e)
        {
            LCustomer customer = (LCustomer)Session["customer"];
            
            LoginView loginview = (LoginView)FindControl("lv");
            TextBox tb_unm = (TextBox)loginview.FindControl("tb_username");
            TextBox tb_upwd = (TextBox)loginview.FindControl("tb_password");
            customer.username = tb_unm.Text;
            customer.password = tb_upwd.Text;
            if (customer.LogIn())
            {

                customer.FillItem();
                FormsAuthentication.SetAuthCookie("C" + customer.Id.ToString(),false);
                Response.Redirect(Request.RawUrl);
            }
        }

 
        protected void btnlogout_Command1(object sender, CommandEventArgs e)
        {
            FormsAuthentication.SignOut();
            LCustomer customer = (LCustomer)Session["customer"];
            customer.LogOff();
            Response.Redirect("home.aspx");
        }
    }
}
